<?php
class model_newagk_platform extends gdl_components_basemodel 
{

    public function primarykey() {
        return 'id';
    }

    public function tableName() {
        return 'agk_platform';
    }

    
    public function getInfoById($platform_id,$field)
    {
        return $this->selectOne(['id' => $platform_id], $field);
    }
	public function getInfoArg($arrg,$field)
    {
        return $this->select($arrg, $field)->items;
    }
	public function getInfoArgOne($arrg,$field)
    {
        return $this->selectOne($arrg, $field);
    }
	
	 public function getInfoByIdadminreg($platform_id,$field)
    {
        return $this->selectOne(['id' => $platform_id], $field);
    }
	public function getListByArgName($platform_id){
		$platform = $this->getInfoArg('id>0','id,reals,admin_reg');
		$platform_name = [];
		foreach($platform as $v){
			$platform_name[$v['admin_reg']] = $v['reals'];
		}
		return $platform_name;
	}
	
	public function getChildAdminRegByPlaformId($platformId,$name=0){
       $admin_regs = [];
	   $condition = 'status=1';
	   if($name){
		   $condition.= " and reals like '%{$name}%'";
	   }
       $result = $this->select($condition,'id,parentid parent_id,admin_reg,level')->items;

        $childs = self::getMenuTree($result,$platformId);
        if (!empty($childs)){
            $admin_regs = array_column($childs,'admin_reg');
            $admin_regs = array_unique($admin_regs);
        }
		$arge = self::getInfoByIdadminreg($platformId,'admin_reg');
		if(!empty( $arge['admin_reg'])){
		$admin_regs[] = $arge['admin_reg'];
		}
    return $admin_regs;

	}
	public function getMenuTree($arrCat, $parent_id = 0, $level = 0)
	{
		static  $arrTree = array(); //使用static代替global
		if( empty($arrCat)) return false;
		$level++;
		foreach($arrCat as $key => $value)
		{
			if($value['parent_id' ] == $parent_id)
			{
				$value['level'] = $level;
				$arrTree[] = $value;
				unset($arrCat[$key]); //注销当前节点数据，减少已无用的遍历
				self::getMenuTree($arrCat, $value['id'], $level);
			}
		}

		return $arrTree;
	}
	
	
	public function getChildByPlaformId($platformId,$res='id'){
        $platform_id = [];
        $result = $this->select(['status'=>1],'id,parentid parent_id,admin_reg,level')->items;

        $childs = self::reverseMenuTreeTwo($result,$platformId);
        if (!empty($childs)){
            $platform_id = array_column($childs,$res);
  
        }
		 $platform_id[] = $platformId;
    return $platform_id;

	}
	
	//方法一 通过不停的查询获取上级
	public function reverseMenuTree($platformId,  $level = 0)
	{
		static  $arrTree = array(); //使用static代替global
		$platform = $this->getInfoById($platformId,'parentid,id');
		$level++;

		
		if(!empty($platform['parentid'])){
			$arrTree[] = $platform['parentid'];
			self::reverseMenuTree($platform['parentid'], $level);
		}

		return $arrTree;
	}
	
	//方法二 一次查询所有一步一步 递归到顶级
	public function reverseMenuTreeTwo($arrCat, $parent_id = 0)
	{
		static  $arrTree = array(); //使用static代替global


		foreach($arrCat as $key => $value)
		{
			if($value['id'] == $parent_id && $value['id'])
			{
				$arrTree[] = $value;
				unset($arrCat[$key]); //注销当前节点数据，减少已无用的遍历
				self::reverseMenuTree($arrCat, $value['parentid']);
			}
		}
		

		return $arrTree;
	}
	
}
